import Vue from 'vue'
import vueRouter from 'vue-router'
Vue.use(vueRouter)
const router = new vueRouter({
    routes: [
        {
            path: '/',
            redirect: { name: 'index' }
        },
        {
            name: 'login',
            path: '/login',
            component: () => import('@/views/login.vue')
        },
        {
            name: 'index',
            path: '/index',
            component: () => import('@/views/index.vue'),
            redirect: { name: 'indexcomponent' },
            children: [
                {
                    name: 'indexcomponent',
                    path: '/indexcomponent',
                    component: () => import('@/views/indexcomponent.vue')
                },
                {
                    name: 'article-add',
                    path: 'article-add',
                    component: () => import('@/views/article-add.vue')
                },
                {
                    name: 'article-lists',
                    path: 'article-lists',
                    component: () => import('@/views/article-lists.vue')
                },
                {
                    name: 'column-add',
                    path: 'column-add',
                    component: () => import('@/views/column-add.vue')
                },
                {
                    name: 'column-lists',
                    path: 'column-lists',
                    component: () => import('@/views/column-lists.vue')
                },
                {
                    name: 'user-add',
                    path: 'user-add',
                    component: () => import('@/views/user-add.vue')
                },
                {
                    name: 'user-lists',
                    path: 'user-lists',
                    component: () => import('@/views/user-lists.vue')
                }
            ]
        }
    ]
})
let arr = ['index']
router.beforeEach((to, from, next) => {
    if (arr.indexOf(to.name)) {
        let token = JSON.parse(localStorage.getItem('hm_62')).token
        if (token) {
            // if () { }
            next()
        } else {
            next({ name: 'login' })
        }
        // 如果用户未能验证身份，则 `next` 会被调用两次
    } else {
        next()
    }

})
export default router